#include "../source/vlib.h"
#include <stdio.h>

static void test_create(void)
{
	map_t map0 = map(string, int);
	map_t map1 = map(int, int); // A map of the int type is similar to the set 
	
	_map(map0);
	_map(map1);
}

static void test_at(void)
{
	map_t map = map(string, int);
	
	map_insert(map, "ZhangSan", la(int, 23));
	map_insert(map, "LiSi", la(int, 22));
	map_insert(map, "WangWu", la(int, 16));
	map_insert(map, "ZhaoLiu", la(int, 32));
	map_insert(map, "SunQi", la(int, 27));
	
	printf("map[WangWu] = %d\r\n", map_at(map, int, "WangWu"));
	map_at(map, int, "WangWu") = 1024;
	printf("map[WangWu] = %d\r\n", map_at(map, int, "WangWu"));

	_map(map);
}

static void test_error(void)
{
	map_t map = map(string, int);
	
	map_insert(map, "ZhangSan", la(int, 23));
	map_insert(map, "LiSi", la(int, 22));
	map_insert(map, "WangWu", la(int, 16));
	map_insert(map, "ZhaoLiu", la(int, 32));
	map_insert(map, "SunQi", la(int, 27));
	
	if (&map_at(map, int, "AAA") == map_error(map))
	{
		printf("AAA error!\r\n");
	}
	
	if (&map_at(map, int, "ZhaoLiu") == map_error(map))
	{
		printf("ZhaoLiu error!\r\n");
	}

	_map(map);
}

static void test_find(void)
{
	map_t map = map(string, int);
	
	map_insert(map, "ZhangSan", la(int, 23));
	map_insert(map, "LiSi", la(int, 22));
	map_insert(map, "WangWu", la(int, 16));
	map_insert(map, "ZhaoLiu", la(int, 32));
	map_insert(map, "SunQi", la(int, 27));
	
	if (map_find(map, "AAA") == 0)
	{
		printf("AAA not found!\r\n");
	}
	
	if (map_find(map, "ZhaoLiu") == 1)
	{
		printf("ZhaoLiu found!\r\n");
	}

	_map(map);
}

static void test_erase(void)
{
	map_t map = map(string, int);
	
	map_insert(map, "ZhangSan", la(int, 23));
	map_insert(map, "LiSi", la(int, 22));
	map_insert(map, "WangWu", la(int, 16));
	map_insert(map, "ZhaoLiu", la(int, 32));
	map_insert(map, "SunQi", la(int, 27));
	
	if (map_find(map, "ZhaoLiu") == 1)
	{
		printf("ZhaoLiu found!\r\n");
	}
	else 
	{
		printf("ZhaoLiu not found!\r\n");
	}
	
	map_erase(map, "ZhaoLiu");
	
	if (map_find(map, "ZhaoLiu") == 1)
	{
		printf("ZhaoLiu found!\r\n");
	}
	else 
	{
		printf("ZhaoLiu not found!\r\n");
	}

	_map(map);
}

static void map_show(map_key_t key, void* data)
{
    printf("map[%s] = %d\r\n", (char *)(key.address), *(int *)data);
}

static void test_traverse(void)
{
	map_t map = map(string, int);
	
	map_insert(map, "ZhangSan", la(int, 23));
	map_insert(map, "LiSi", la(int, 22));
	map_insert(map, "WangWu", la(int, 16));
	map_insert(map, "ZhaoLiu", la(int, 32));
	map_insert(map, "SunQi", la(int, 27));
	
	map_traverse(map, map_show);

	_map(map);
}

void test_map(void)
{
    // test_create();
	// test_at();
	// test_error();
	// test_find();
	// test_erase();
	test_traverse();
}
